﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using BLL;
using DAL;
using DataProj.Areas.BackSys.ServiceTools;
using Model;
using Newtonsoft.Json;

namespace DataProj.Areas.BackSys.Controllers
{
    public class DeliverymanController : BaseController
    {
        // GET: Deliveryman
        private DeliverymanImpl _impl;
        //
        // GET: /BackSys/Deliveryman/

        public DeliverymanController()
        {
            if (_impl == null) _impl = new DeliverymanImpl();
            if (string.IsNullOrEmpty(MySession.GetSession("deliverymanRows")))
            {
                MySession.WriteSession("deliverymanRows", 10);
            }
            if (string.IsNullOrEmpty(MySession.GetSession("deliverymanPage")))
            {
                MySession.WriteSession("deliverymanPage", 1);
            }

        }
        public ActionResult Index()
        {
            return PartialView(_impl.GetList());
        }

        public ActionResult DeliverymanRegister()
        {
            return PartialView();
        }

        public ActionResult Profile()
        {
            return PartialView();
        }


        [HttpPost]
        public ActionResult ChangePage()
        {
            IDictionary<string, string> set = new Dictionary<string, string>();
            int page = Convert.ToInt16(Request["pageNum"].Trim());
            MySession.WriteSession("deliverymanPage", page);
            switch (LoginType)
            {
                case "用户":
                case "快递员":
                    return RedirectToAction("Index", "Home");
                case "管理员":
                    (var totalPages, var data) = _impl.GetList(Convert.ToInt16(MySession.GetSession("deliverymanRows")), page);
                    set.Add("totalPages", totalPages.ToString());
                    set.Add("data", JsonConvert.SerializeObject(data));
                    return Json(JsonConvert.SerializeObject(set));
                    //MySession.WriteSession("deliverymanTotalPages", totalPages);
                    //return Json(JsonConvert.SerializeObject(data));
                    //ViewBag.DeliverymanList = data;
                    //return PartialView();
                default:
                    break;
            }

            return Json(0);
        }



        public ActionResult DeliverymanManage()
        {

         
                switch (LoginType)
                {
                    case "用户":
                        return RedirectToAction("Index", "Home");
                    case "快递员":
                    case "管理员":
                    //(var totalPages, var data) = _impl.GetList(Convert.ToInt16(MySession.GetSession("deliverymanRows")), Convert.ToInt16(MySession.GetSession("deliverymanPage")));
                    //MySession.WriteSession("deliverymanTotalPages", totalPages);
                    //ViewBag.DeliverymanList = data;
                        return PartialView();
                    default:
                        break;
                }
           
            return PartialView();
        }
        public ActionResult DeliverymanUpdate()
        {
            return PartialView();
        }
        [HttpPost]
        public ActionResult Update()
        {
            int id = Convert.ToInt32(Request["ID"]);
            Deliveryman deliveryman = _impl.Find(id);
            if (deliveryman == null)
            {
                return Json(0);
            }
            deliveryman.UserName = Request["UserName"];
            deliveryman.UserPass = Request["UserPass"];
            deliveryman.Phone = Request["Phone"];
            deliveryman.TrueName = Request["TrueName"];
            deliveryman.IDCard = Request["IDCard"];
            deliveryman.UpdateTime = DateTime.Now;

            if(_impl.Update(id,deliveryman))return Json(1);

            return Json(0);
        }

        [HttpPost]
        public ActionResult GetItemByID(string id,string action)
        {
            var tool = new JavaScriptSerializer();
            //string get = MyRequest.GetQueryString();
            //Task obj = tool.DeserializeObject(get) as Task;
            var result = _impl.GetItemByID(Convert.ToInt32(id));
            string seri = tool.Serialize(result);
            return Json(seri);
        }
        public ActionResult List()
        {
            int page = 1;
            if (!string.IsNullOrWhiteSpace(Request["deliverymanPage"]))
            {
                if (int.TryParse(Request["deliverymanPage"], out page))
                {
                    if (page < 1)
                    {
                        page = 1;
                    }
                };
            }
            int rows = 10;
            if (!string.IsNullOrWhiteSpace(Request["deliverymanRows"]))
            {
                if (int.TryParse(Request["deliverymanRows"], out rows))
                {
                    if (rows < 10)
                    {
                        rows = 10;
                    }
                    else if (rows > 30)
                    {
                        rows = 30;
                    }
                }
            }
            return Json(new { rows = _impl.GetList(rows,page), total = _impl.ExistNum },
                JsonRequestBehavior.AllowGet);
            //return Json(db.Deliverymen.Skip((page-1)*rows).Take(rows),JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /BackSys/Deliveryman/Details/5

        public ActionResult Details(int id = 0)
        {
            Deliveryman deliveryman = _impl.Find(id); 
            if (deliveryman == null)
            {
                return HttpNotFound();
            }
            return PartialView(deliveryman);
        }

        //
        // GET: /BackSys/Deliveryman/Create

        public ActionResult Create()
        {
            return PartialView();
        }

        //
        // POST: /BackSys/Deliveryman/Create
        [HttpPost]
        public ActionResult Add()
        {
            Deliveryman deliveryman = new Deliveryman();
            deliveryman.UserName = Request["UserName"];
            deliveryman.UserPass = Request["UserPass"];
            deliveryman.Phone = Request["Phone"];
            deliveryman.TrueName = Request["TrueName"];
            deliveryman.IDCard = Request["IDCard"];
            deliveryman.AddTime = DateTime.Now;
            if(_impl.Add(deliveryman)==true)return Json(1) ;
            return Json(0);
        }

        [HttpPost]
        public ActionResult Create(Deliveryman deliveryman)
        {
            if (ModelState.IsValid)
            {
                _impl.Add(deliveryman);
            
                return RedirectToAction("Index");
            }

            return PartialView(deliveryman);
        }

        //
        // GET: /BackSys/Deliveryman/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Deliveryman deliveryman = _impl.Find(id);
            if (deliveryman == null)
            {
                return HttpNotFound();
            }
            return PartialView(deliveryman);
        }

        //
        // POST: /BackSys/Deliveryman/Edit/5

        //[HttpPost]
        //public ActionResult Edit(Deliveryman deliveryman)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        _impl.Update(deliveryman);
             
        //        return RedirectToAction("Index");
        //    }
        //    return PartialView(deliveryman);
        //}
        public ActionResult Login()
        {
            Deliveryman deliveryman = new Deliveryman();
            deliveryman.UserName = Request["UserName"].Trim();
            //if(
            deliveryman.UserPass = Request["UserPass"].Trim();


            Deliveryman find = _impl.Login(deliveryman.UserName, deliveryman.UserPass);
            if (find == null)
            {
                return Content(Common.FAIL.ToString());
            }
            else
            {
                deliveryman.UserPass = null;
                Session[Common.DeliveryLoginSessionKey] = deliveryman;
                return Content(Common.SUCCESS.ToString());
            }
        }
        //
        // GET: /BackSys/Deliveryman/Delete/5
        [HttpPost]
        public ActionResult Delete(string id, string action)
        {
            //Deliveryman deliveryman = db.Deliverymen.Select(o=>o.Phone=="158888888");
            if (_impl.Delete(Convert.ToInt32(id)) == true)
            {
                return Json(1);
            }

            return Json(0);
        }

        public ActionResult Remove(int id = 0)
        {
            //int id = 0;
            //if (int.TryParse(Request["ID"], out id))
            //{
            _impl.Delete(id);
            return Content(Common.SUCCESS.ToString());
            //}
            //return Content(Common.FAIL.ToString());
        }
        //
        // POST: /BackSys/Deliveryman/Delete/5



        protected override void Dispose(bool disposing)
        {
            _impl.Dispose();
            base.Dispose(disposing);
        }
    }
}